package com.zq.solution;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName SearchInsert
 * @Description 搜索插入位置
 * 给定一个排序数组和一个目标值，在数组中找到目标值，并返回其索引。如果目标值不存在于数组中，返回它将会被按顺序插入的位置。
 * 示例：
 *   输入: [1,3,5,6], 5
 *   输出: 2
 * @Author ZQ
 * @Date 2020/10/13 16:34
 * @Version 1.0
 */
public class SearchInsert {
    public static int searchInsert1(int[] nums, int target) {
        int n = nums.length;
        int left = 0, right = n - 1, ans = n;
        while (left <= right) {
            int mid = ((right - left) >> 1) + left;
            if (target <= nums[mid]) {
                ans = mid;
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return ans;
    }

    public static int searchInsert2(int[] nums, int target) {
        List<Integer> numsList = Arrays.asList();
        return 0;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,5,6};
        int target = 7;
        System.out.println(searchInsert1(nums,target));
    }
}
